package com.tech.algorithm.learning.chapter2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 通过ArrayList的方式实现约瑟夫环的问题
 * @Author: zl
 * @Date: 2019/8/1 10:09
 */
public class Josephus01 {
    public static void main(String[] args) {
        int  start = 0, distance = 2;
        String string = "A,B,C,D,E";
        List<String> list = new ArrayList<>();//Arrays.asList(string.split(","));
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        int i = start;                                     //计数起始位置
        while (list.size() > 1)                              //多于一个元素时循环，计数O(1)
        {
            i = (i + distance - 1) % list.size();              //按循环方式对顺序表进行遍历
            System.out.print("删除" + list.remove(i).toString() + "，");  //删除i位置对象，O(n)
            System.out.println(list.toString());
        }
        System.out.println("被赦免者是" + list.get(0).toString());//get(0)获得元素，O(1)


    }
}
